package com.hp.usage.gr;

import com.hp.siu.collector.rules.Rule;
import com.hp.siu.collector.rules.RuleException;
import com.hp.siu.utils.*;
import com.hp.siu.utils.NMESchema;
import com.hp.usage.nme.*;
import com.hp.usage.nme.NMEType;

/**
 * Created by huxiaoji on 1/28/2016.
 */
public abstract class GrBaseRule extends Rule{
    protected NMESchema nmeSchema_ = NMESchema.getInstance();
    private AttributeInfo snmeTypeNameAttributeInfo_ = null;
    private int snmeTypeNameIndex_ = -1;

    protected boolean isEmpty(String value){
        if (value == null) return true;
        return value.trim().equals("");
    }

    protected void setAttributesInNME(NormalizedMeteredEvent nme, String nmeName, String value) {
        Attribute attr = nme.getAttribute(nmeName);
        if (attr == null) {
            attr = nmeSchema_.newAttributeInstance(nmeName);
        }
        attr.setValue(value);
        nme.setAttribute(attr,nmeName);
    }

    protected NME getSNME(NormalizedMeteredEvent nme){
        NMEAdapter localNMEAdapter = (NMEAdapter)nme;
        NME snme = localNMEAdapter.getNME();

        return snme;
    }

    protected void next(NormalizedMeteredEvent nme, NormalizedMeteredEvent nme1) throws RuleException {
        if (this.next_ != null) this.next_.applyRule(nme, nme1);
    }

    protected NMEMutableArray createNewArray(int num){
        return NMEManager.getNMEFactory().getArrayAllocator().newNMEArray(num);
    }

    protected NME createEmptyNME(String type)
            throws NamespaceNotFoundException, NMETypeNotFoundException, NMEException
    {
        NMEType paramNMEType = NMEManager.getNMESchema().getNMEType(type);
        return NMEManager.getNMEFactory().newNME(paramNMEType);
    }
//
//    protected void copySNMETypeName(NMEAdapter paramNMEAdapter)
//    {
//        if (this.snmeTypeNameAttributeInfo_ != null)
//        {
//            NME localNME = paramNMEAdapter.getNME();
//            StringAttribute localStringAttribute = (StringAttribute)paramNMEAdapter.getAttribute(this.snmeTypeNameIndex_);
//            if (localStringAttribute == null)
//            {
//                localStringAttribute = (StringAttribute)this.snmeTypeNameAttributeInfo_.newAttributeInstance();
//                paramNMEAdapter.setAttribute(localStringAttribute, this.snmeTypeNameIndex_);
//            }
//            if (localNME != null)
//            {
//                com.hp.usage.nme.NMEType localNMEType = localNME.getType();
//                localStringAttribute.setValue(localNMEType.getQualifiedName());
//            }
//            else
//            {
//                localStringAttribute.setEmptyValue();
//            }
//        }
//    }

    protected void setLong(NME snme, String name, String value){
        AttributeRef valRef = snme.getAttributeRef(name);
        NME val;
        if (snme.isAttributeSet(valRef)){
            val = snme.getStruct(valRef);
        } else {
            val = createEmptyNME("gr:" + valRef.getType().getName());
            snme.setStruct(valRef, val);
        }
        val.setLong(val.getAttributeRef("CData"), Long.parseLong(value));
    }

    protected void setInt(NME snme, String name, String value){
        AttributeRef valRef = snme.getAttributeRef(name);
        NME val;
        if (snme.isAttributeSet(valRef)){
            val = snme.getStruct(valRef);
        } else {
            val = createEmptyNME("gr:" + valRef.getType().getName());
            snme.setStruct(valRef, val);
        }
        val.setInt(val.getAttributeRef("CData"), Integer.parseInt(value));
    }

    protected void setString(NME snme, String name, String value){
        AttributeRef valRef = snme.getAttributeRef(name);
        NME val;
        if (snme.isAttributeSet(valRef)){
            val = snme.getStruct(valRef);
        } else {
            val = createEmptyNME("gr:" + valRef.getType().getName());
            snme.setStruct(valRef, val);
        }
        val.setString(val.getAttributeRef("CData"), value==null?"":value);
    }

    protected NME getResponse(NME snme){
        AttributeRef ref = snme.getAttributeRef("response");
        NME snmeApdsResponse;
        if (snme.isAttributeSet(ref)){
            snmeApdsResponse = snme.getStruct(ref);
        } else {
            snmeApdsResponse = createEmptyNME("gr:ResponseType");
            snme.setStruct(ref, snmeApdsResponse);
        }
        return snmeApdsResponse;
    }

    protected void setResultCode(NME snme, String code){
        NME snmeApdsResponse = getResponse(snme);
        setLong(snmeApdsResponse, "resultCode", code);
    }
}
